package com.dst.smart.dao.impl.factory;

import java.util.ArrayList;
import java.util.List;
import com.dst.smart.app.MyApplication;
import com.dst.smart.model.VoiceInfo;
import com.dst.smart.service.SendCMD;

public class VoiceFactory {
	public static List<VoiceInfo> list = new ArrayList<VoiceInfo>();

	public static List<VoiceInfo> getAll() {
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getVoice();
		}
		return list;
	}

	public static List<VoiceInfo> getVoiceInfo(String voiceName,String pinying) {
		List<VoiceInfo> resultList = new ArrayList<VoiceInfo>();
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getVoice();
		}
		for (VoiceInfo info : list) {
			if(!info.getUserName().equals("1")) {
				String inFoName = info.getVoiceName().replace("，", ",") + ",";
				if (inFoName.contains("+")) {
					int ActionFlag = 0;
					inFoName = inFoName.replace("+", "");
					String[] ActFun = inFoName.split(",");
					for (String str : ActFun) {
						if (str.length() > 0) {
							if (voiceName.contains(str) || SendCMD.getInstance().PinYinComparison(pinying, str)) {
								ActionFlag++;
							}
						}
					}
					if (ActionFlag > 1) {
						resultList.add(info);
					}
				} else {
					String[] ActFun = inFoName.split(",");
					for (String str : ActFun) {
						if (str.length() > 0) {
							if (voiceName.contains(str) || SendCMD.getInstance().PinYinComparison(pinying, str)) {
								resultList.add(info);
							}
						}
					}
				}
			}
		}
		/*
		for (VoiceInfo info : list) {
			String inFoName = info.getVoiceName().replace("，", ",");
			if(inFoName.indexOf( ",")!=-1){
				String[] ActFun = inFoName.split(",");
				int ActionFlag = 0;
				for (int i = 0; i < ActFun.length; i++) {
					if (voiceName.indexOf(ActFun[i]) != -1) {// 有识别词
						inFoName=inFoName.replace(ActFun[i], "");
						ActionFlag++;
					}
					//else{
					//	String pinyingDev=SendCMD.getPinYin(ActFun[i]);
					//	if ((!pinyingDev.equals("") && (pinyingDev.length() > 0))) {
					//		if (pinying.indexOf(pinyingDev) != -1) {
					//			ActionFlag++;
					//		}
					//	}
					//}
				}
				if(ActionFlag>1){
					resultList.add(info);
				}
			}else{
				if (info.getVoiceName().indexOf(voiceName) != -1
						|| voiceName.indexOf(info.getVoiceName()) != -1) {
					resultList.add(info);
				}else{
					String pinyingDev=SendCMD.getPinYin(info.getVoiceName());
					if ((!pinyingDev.equals("") && (pinyingDev.length() > 0))) {
						if (pinying.indexOf(pinyingDev) != -1) {
							resultList.add(info);
						}
					}
				}
			}
		}
		*/
		return resultList;
	}

	public static int isExite(String voiceName, String voiceAnswer) {
		int number = 0;
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getVoice();
		}
		for (VoiceInfo info : list) {
			if (info.getVoiceName().equals(voiceName)
					&& info.getVoiceAnswer().equals(voiceAnswer)) {
				number = 1;
				break;
			}
		}
		return number;
	}

	public static int isExite(int Id) {
		int number = 0;
		if (list.size() == 0) {
			list = MyApplication.getInstance().getWidgetDataBase().getVoice();
		}
		for (VoiceInfo info : list) {
			if (info.getId() == Id) {
				number = 1;
				break;
			}
		}
		return number;
	}

	public static void clearList() {
		list = MyApplication.getInstance().getWidgetDataBase().getVoice();
	}
}
